/* jshint unused: false */

const nunjucks = require('nunjucks')

function createEnv(path, opts) {
  let
    autoescape = opts.autoescape === undefined ? true : opts.autoescape,
    noCache = opts.noCache || false,
    watch = opts.watch || false,
    throwOnUndefined = opts.throwOnUndefined || false,
    env = new nunjucks.Environment(
      new nunjucks.FileSystemLoader(path || 'views', {
        noCache: noCache,
        watch: watch,
      }), {
        autoescape: autoescape,
        throwOnUndefined: throwOnUndefined
      })

  const filters = opts.filters
  if (filters) {
    const keys = Object.keys(filters)
    for (var f of keys) {
      env.addFilter(f, filters[f])
    }
  }
  return env
}

/* jshint ignore:start */
function templating(path, opts) {
  // 创建Nunjucks的env对象:
  var env = createEnv(path, opts)
  
  return async (ctx, next) => {
    // 给ctx绑定render函数:
    ctx.render = function (view, model) {
      // 把render后的内容赋值给response.body:
      ctx.response.body = env.render(view, Object.assign({}, ctx.state || {}, model || {}))
      // 设置Content-Type:
      ctx.response.type = 'text/html'
    }
    // 继续处理请求:
    await next()
  }
}
/* jshint ignore:start */

module.exports = templating